package com.wy.web;

import com.wy.pojo.Contact;
import com.wy.service.ContactService;
import com.wy.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/contact")
public class ContactServlet extends BaseServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.doPost(req, resp);
//    }

//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1.获取请求标记
//        String method = req.getParameter("method");
//        //2.判断请求标记
//        if("findAll".equals(method)){
//            findAll(req,resp);
//        }else if("addContact".equals(method)){
//            addContact(req,resp);
//        }else if("findContactById".equals(method)){
//            findContactById(req,resp);
//        }else if("updateContact".equals(method)){
//            updateContactById(req,resp);
//        }else if("deleteContactById".equals(method)){
//            deleteContactById(req,resp);
//        }else{
//            System.out.println("您访问的资源不存在");
//        }
//    }

    public void deleteContactById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.获取请求携带的id值
        String id = req.getParameter("id");
        //2.调用service处理业务
        ContactService service = new ContactService();
        boolean flag = service.deleteContactById(id);
        //3.判断执行结果,生成响应信息
        if(flag){
            //重定向查询所有联系人信息
            resp.sendRedirect(req.getContextPath()+"/contact?method=findAll");
            System.out.println(req.getContextPath()+"/contact/findAll");
        }else{
            //重定向到错误页面进行委婉提示
            resp.sendRedirect(req.getContextPath()+"/error.jsp");

        }
    }

    public void updateContactById(HttpServletRequest req, HttpServletResponse resp) {
        try{
            Map<String, String[]> map = req.getParameterMap();
            Contact contact = new Contact();
            BeanUtils.populate(contact,map);
            ContactService contactService = new ContactService();
            boolean flag=contactService.updateContact(contact);
            if(flag){
                //重定向查询所有联系人信息
                resp.sendRedirect(req.getContextPath()+"/contact?method=findAll");
            }
//           else{
//               //重定向到错误页面进行委婉提示
//               req.setAttribute("msg","当前功能正在维护");
//               resp.sendRedirect("/contact/update.jsp");
//           }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void findContactById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        ContactService contactService = new ContactService();
        Contact contact =contactService.findContactById(id);
        req.setAttribute("contact",contact);
        req.getRequestDispatcher("/contact/update.jsp").forward(req,resp);
    }

    public void addContact(HttpServletRequest req, HttpServletResponse resp) {
        try{
            Map<String, String[]> map = req.getParameterMap();
            Contact contact = new Contact();
            BeanUtils.populate(contact,map);
            System.out.println("请求携带的数据"+contact);

            ContactService contactService = new ContactService();
            boolean flag = contactService.addContact(contact);
            if(flag){
                resp.sendRedirect(req.getContextPath()+"/contact/findAll");
            }else{
                req.setAttribute("msg","当前功能正在维护");
                req.getRequestDispatcher("/contact/add.jsp");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.调用service处理业务逻辑
        ContactService service = new ContactService();
        List<Contact> list = service.findAll();
        for (Contact contact : list) {
            System.out.println(contact);
        }
        //2.将查询结果存放到request对象中
        req.setAttribute("list",list);
        //3.请求转发到jsp页面
        req.getRequestDispatcher("/contact/list.jsp").forward(req,resp);
    }
}
